import Vue from 'vue'
import Login from './components/login'
import Home from './components/Home'
import Router from 'vue-router'
import Welcome from './components/Welcome'
import Users from './components/user/Users'
import Rights from './components/quanxian/Rights'
import UserList from './components/quanxian/UserList'
import Cate from './components/goods/Cate'
import Params from './components/goods/Params'
import List from './components/goods/List'
import AddGoods from './components/goods/AddGoods'
import Order from './components/order/order'
import Report from './components/report/report'
Vue.use(Router)

const router = new Router({
    routes: [
        { path: '/', redirect: '/login', },
        { path: '/login', component: Login, meta: { title: '电商登陆首页' } },
        {
            path: '/home',
            component: Home,
            redirect: '/Welcome',
            children: [
                { path: '/Welcome', component: Welcome, meta: { title: '电商管理平台' } },
                { path: '/users', component: Users, meta: { title: '人员管理' } },
                { path: '/rights', component: Rights, meta: { title: '权限管理' } },
                { path: '/roles', component: UserList, meta: { title: '用户列表' } },
                { path: '/categories', component: Cate, meta: { title: '商品分类' } },
                { path: '/params', component: Params, meta: { title: '分类参数' } },
                { path: '/goods', component: List, meta: { title: '人商品列表' } },
                { path: '/goods/addGoods', component: AddGoods, meta: { title: '添加商品' } },
                { path: '/orders', component: Order, meta: { title: '订单列表' } },
                { path: '/reports', component: Report, meta: { title: '数据报表' } },



            ]
        }
    ]
})

//挂载路由守卫  全局前置路由守卫
router.beforeEach((to, from, next) => {
    //如果用户访问登陆页，直接放行
    if (to.path === '/login') return next()
        //从sessionStorage中获取到保存的token值
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
        //如果有token放行
    next()
})

//全局后置路由守卫
router.afterEach((to, from, next) => {
    // console.log(to)
    document.title = to.meta.title || '电商登陆首页'
        // next()
})
export default router